import 'package:flutter/material.dart';

//弹出后不消失的弹窗.
class OverlayWidget {

  //静态访问
  static show({BuildContext context, String msg}) {

    //是否关闭页面.
    final bool isDismiss = false;
    //实现弹出逻辑.
    var overlayState = Overlay.of(context);

    //构建布局子控件.
    OverlayEntry overlayEntry;
    overlayEntry = new OverlayEntry(builder: (context) {
      return buildToastLayout(msg: msg,isDismiss: isDismiss);
    });

    var overlayView = OverlayView();
    overlayView.overlayEntry = overlayEntry;
    overlayView.overlayState = overlayState;


    //insert可以让页面置于顶层.
//    overlayState.insert(overlayEntry);
  overlayView._show();
  }

  static LayoutBuilder buildToastLayout({String msg, bool isDismiss}) {
    //构建布局
    return LayoutBuilder(builder: (context, constraints) {
      return Container(
//          width: 400,
          child: Material(
            color: Colors.white.withOpacity(0),
            child: Container(
              child: Row(children: <Widget>[
                Icon(Icons.print),
                Container(
                  child: Text(
                    "${msg}",
                    style: TextStyle(color: Colors.white),
                  ),
                  decoration: BoxDecoration(
                    color: Colors.black.withOpacity(0.6),
                    borderRadius: BorderRadius.all(
                      Radius.circular(5),
                    ),
                  ),
                  padding: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
                ),
                GestureDetector(child: Icon(Icons.close),onTap: (){
                  print('hok我');

                },)
              ],),
              margin: EdgeInsets.only(
                bottom: constraints.biggest.height * 0.15,
                left: constraints.biggest.width * 0.2,
                right: constraints.biggest.width * 0.2,
              ),
            ),
          ),
          alignment: Alignment.bottomCenter,
        );
    });
  }
}


class OverlayView {
  OverlayEntry overlayEntry;
  OverlayState overlayState;
  bool dismissed = false;

  //2秒后消失.
  _show() async {
    overlayState.insert(overlayEntry);
//    await Future.delayed(Duration(milliseconds: 10000));
    this.dismiss();
  }

  dismiss() async {
    if (dismissed) {
      return;
    }
    this.dismissed = true;
    overlayEntry?.remove();
  }
}